﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class SupplierManager
    {
        public Supplier GetSupplierByCode(string code)
        {
            using (var db = new QLKDSDataContext())
            {
                var querySupplier = from d in db.Suppliers
                                    where d.Status.Equals(1)
                                    where d.Code.Equals(code)
                                    select d;
                return querySupplier.OrderBy(d => d.Code).First();
            }

        }

        public SupplierInfo GetSupplierById(long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var querySupplier = from d in db.Suppliers
                                    where d.Status.Equals(1)
                                    where d.Id.Equals(id)
                                    select new SupplierInfo
                                    {
                                        Id = d.Id,
                                        Code = d.Code,
                                        Name = d.Name,
                                        Liabilities=(float)d.Liabilities,
                                        Status = d.Status,
                                        Address = d.Address,
                                        Fax = d.Fax,
                                        Website = d.Website,
                                        TaxCode = d.TaxCode,
                                        Mobile = d.Mobile,
                                        Phone = d.Phone,
                                        Email = d.Email,
                                        Description = d.Description,
                                        CreatedBy = d.CreatedBy,
                                        CreatedDate = d.CreatedDate,
                                        ModifiedBy = d.ModifiedBy,
                                        ModifiedDate = d.ModifiedDate,
                                        Contactor = d.Contactor,
                                        ContactorPhone = d.ContactorPhone,
                                        ContactorEmail = d.ContactorEmail,
                                        ContactorMobile = d.ContactorPhone
                                    };

                return querySupplier.OrderBy(d => d.Code).First();
            }

        }

        public IList<SupplierInfo> ListSupplier()
        {
            using (var db = new QLKDSDataContext())
            {
                var querySupplier = from d in db.Suppliers
                                    where d.Status.Equals(1)
                                    select new SupplierInfo { Id = d.Id, Code = d.Code, Name = d.Name, Status = d.Status, Address = d.Address, Fax = d.Fax, Website = d.Website, TaxCode = d.TaxCode, Mobile = d.Mobile, Phone = d.Phone, Email = d.Email, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate, Contactor = d.Contactor, ContactorPhone = d.ContactorPhone, ContactorEmail = d.ContactorEmail, ContactorMobile = d.ContactorPhone };
                return querySupplier.OrderBy(d => d.Code).ToList();
            }
        }

        public void CreateSupplier(Supplier Supplier)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Suppliers.InsertOnSubmit(Supplier);

                db.SubmitChanges();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="supplier"></param>
        public void UpdateSupplier(Supplier supplier)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Supplier = db.Suppliers.Single(d => d.Id.Equals(supplier.Id));

                _Supplier.Code = supplier.Code;
                _Supplier.Name = supplier.Name;
                _Supplier.Liabilities = supplier.Liabilities;
                _Supplier.Address = supplier.Address;
                _Supplier.Mobile = supplier.Mobile;
                _Supplier.Phone = supplier.Phone;
                _Supplier.Email = supplier.Email;
                _Supplier.Status = supplier.Status;
                _Supplier.Fax = supplier.Fax;
                _Supplier.Website = supplier.Website;
                _Supplier.TaxCode = supplier.TaxCode;
                _Supplier.Description = supplier.Description;
                _Supplier.CreatedBy = supplier.CreatedBy;
                _Supplier.CreatedDate = supplier.CreatedDate;
                _Supplier.ModifiedBy = supplier.ModifiedBy;
                _Supplier.ModifiedDate = supplier.ModifiedDate;
                _Supplier.Contactor = supplier.Contactor;
                _Supplier.ContactorEmail = supplier.ContactorEmail;
                _Supplier.ContactorMobile = supplier.ContactorMobile;
                _Supplier.ContactorPhone = supplier.ContactorPhone;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteSupplier(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Supplier = db.Suppliers.Single(d => d.Id.Equals(id));

                _Supplier.Status = 0;

                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void RemoveSupplier(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Supplier = db.Suppliers.Single(d => d.Id.Equals(id));

                db.Suppliers.DeleteOnSubmit(_Supplier);
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool HasCodeExisted(string code)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Suppliers
                            where d.Code.Equals(code)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasCodeDuplicated(string code, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Suppliers
                            where d.Code.Equals(code)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public bool HasMobileExisted(string mobile)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Suppliers
                            where d.Mobile.Equals(mobile)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasMobileDuplicated(string mobile, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Suppliers
                            where d.Mobile.Equals(mobile)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool HasEmailExisted(string email)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Suppliers
                            where d.Email.Equals(email)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="email"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasEmailDuplicated(string email, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Suppliers
                            where d.Email.Equals(email)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }
    }
}
